package base.thread.state.sleep.networkDelay;

/**
 * @Author: my_code
 * @Create: 2020-12-08 12:29
 * <p>
 * 模拟网络拥塞
 *      ：ticket重复售卖，引起的线程安全问题
 *
 */
public class TicketSale implements Runnable {

    private int ticket = 10;

    public static void main(String[] args) {
        TicketSale ticketSale = new TicketSale();
        new Thread(ticketSale,"A").start();
        new Thread(ticketSale,"B").start();
        new Thread(ticketSale,"C").start();
    }

    @Override
    public void run() {
        while (true){
            if (ticket <= 0){
                break;
            }
            System.out.println(Thread.currentThread().getName() + "购买了第" + ticket-- + "票");
            //延时几秒
            try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
        }
    }
}
